package com.cloudpivot.receivales.ding.robot.service.baidu;

import com.alibaba.fastjson.JSONObject;
import com.cloudpivot.receivales.ding.basic.service.impl.HttpAPIServiceForDing;
import com.cloudpivot.receivales.ding.robot.domain.ErnieMessageDTO;
import com.cloudpivot.receivales.ding.robot.domain.ErnieRequestDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 百度 文心大模型 服务
 * 使用rest 调用
 */
@Service
@Slf4j
public class BaiduERNIEServiceImpl {

    /**
     * 对应百度 文本相似度的模型
     */
    private String APIKEY = "ld2zhtL6KxdQTeK7TBgOHTnG";
    private String SECRETKEY = "s0DLNUWQ9ULo9aAPELDDsuGhsxPyBUFX";

    @Autowired
    private BaiduAuthServiceImpl baiduAuthService;

    /**
     * 百度 文心4.0  preview
     */
    public JSONObject ernie4PreView(List<ErnieMessageDTO> dto) {
        String accessToken = baiduAuthService.getToken(APIKEY, SECRETKEY);

        String baseUrl = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-4.0-turbo-8k";
        String requestUrl = String.format("%s?access_token=%s", baseUrl, accessToken);

        //组合对象
        ErnieRequestDTO ernieRequestDTO = new ErnieRequestDTO();
        ernieRequestDTO.setMessages(dto);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> req = new HttpEntity<>(JSONObject.toJSONString(ernieRequestDTO), headers);
        JSONObject retJson = restTemplate.postForObject(requestUrl, req, JSONObject.class);
        System.out.println("打印报文---->>>>"+JSONObject.toJSONString(retJson));
        return retJson;
    }

}
